import { defineStore } from 'pinia'

export const useHistoryStore = defineStore('history', {
  state: () => ({
    historyChatList: [],
    historyChatListAry: [],
    title: ''
  }),
  actions: {
    pushHistory(ChatList, ChatListAry?) {
      const newArr = []
      const msg = ChatList.map((item: any, i: number) => {
        newArr.push({
            sessionId: item.sessionId,
            name: item.name,
            history: []
        })

        for (let index = 0; index < item.msg.length; index++) {
            const element = item.msg[index];
            if (index % 2) {
                newArr[i].history[newArr[i].history.length - 1].res = element.replace('MY: ', '')
            } else {
                newArr[i].history.push({
                    req: element.replace('MY: ', '')
                })
            }
        }
      });

      console.log(newArr);
      
      this.historyChatList = newArr
      this.historyChatListAry = ChatListAry
    },
    changeTitle(sessionId) {
        console.log(sessionId);
        
        this.title = this.historyChatList.find((item: any) => item.sessionId === sessionId)?.name
    }
  },
})
